#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

void worker()
{

}

int main()
{
    pid_t id = fork();
    if(id == 0) 
    {
        //child
        int cnt = 5;
        while(cnt--)
        {
            worker();
            sleep(2);
            exit(0);
        }
    }

    while(1)//轮询
    {
        //father
        int status = 0;
        pid_t rid = waitpid(id, &status, WNOHANG);//非阻塞
        if(rid > 0) 
        {
            //等待成功，子进程已退出
            printf("child quit success, exit_code: %d, exit_signal: %d\n", (status>>8)&0xFF, status&0x7F);
            break;
        }
        else if(rid == 0)
        {
            //等待成功， 子进程未退出
            printf("father do other thing ...\n");
        }
        else
        {
            //等待失败
            printf("wait failed\n");
        }
        sleep(1);
    }

    return 0;
}
